﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Guitar.CustomControls
{

	public partial class NumericUpDown : UserControl
    {

        #region Properties

        public Int32 MinValue { get; set; }
        public Int32 MaxValue { get; set; }

        public Int32 Value
        {
            get { return (Int32)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); textBoxValue.Text = Convert.ToString(value); }
        }

        #endregion

        public NumericUpDown()
        {
            InitializeComponent();

            MinValue = 0;
            MaxValue = 100;

            this.LayoutRoot.DataContext = this;
        }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Int32), typeof(NumericUpDown), new FrameworkPropertyMetadata((Int32)0, new PropertyChangedCallback(OnValueChanged), new CoerceValueCallback(CoerceValue)));

        private static Object CoerceValue(DependencyObject element, Object value)
        {
            Int32 newValue = (Int32)value;
            NumericUpDown nudCtrl = element as NumericUpDown;

            // On controle les bornes
            return Math.Max(nudCtrl.MinValue, Math.Min(nudCtrl.MaxValue, newValue));
        }

        private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            NumericUpDown nudCtrl = obj as NumericUpDown;
            nudCtrl.OnValueChanged(new RoutedPropertyChangedEventArgs<Int32>((Int32)args.OldValue, (Int32)args.NewValue, ValueChangedEvent));
        }

    
        public static readonly RoutedEvent ValueChangedEvent =
            EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<Int32>),
            typeof(NumericUpDown));

        public event RoutedPropertyChangedEventHandler<Int32> ValueChanged
        {
            add { AddHandler(ValueChangedEvent, value); }
            remove { RemoveHandler(ValueChangedEvent, value); }
        }

        protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<Int32> args)
        {
            RaiseEvent(args);
        }

        #region Up & Down Buttons Actions

        private void btUpButton_Click(object sender, EventArgs e)
        {
            Value++;
        }

        private void btDownButton_Click(object sender, EventArgs e)
        {
            Value--;
        }

        #endregion

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // On controle que le texte saisie est bien un entier non vide
            int i=0;
            if (textBoxValue.Text.Length != 0)
            {
                if (!Int32.TryParse(textBoxValue.Text, out i))
                {
                    MessageBox.Show("Format Invalide");

                }
                else
                {
                    if (i <= MaxValue)
                    {
                        Value = i;
                    }
                    else
                    {
                        Value = MaxValue;
                        textBoxValue.Text = Convert.ToString(Value);
                    }

                }

            }
            else
            {
                Value = MinValue;
                textBoxValue.Text = Convert.ToString(Value);
            }
        }
    
    }
}